package latihan.action;

import java.util.Map;
import java.util.logging.Logger;

import latihan.sql.LoginManager;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;

import com.opensymphony.xwork2.ActionContext;

@Results({
	@Result(name=MainController.LOGIN, location="/WEB-INF/pages/login.jsp"),
	@Result(name=MainController.GREETINGS, location="/WEB-INF/pages/greetings.jsp"),
	@Result(name=MainController.REGISTRATION, location="/WEB-INF/pages/registrationForm.jsp")
})

@ParentPackage("default")
@Namespace("/main")
public class MainController extends BaseController {
	/**
	 * 
	 */

	public Logger log = Logger.getLogger(MainController.class.getName());
	private static final long serialVersionUID = 1L;
	public final static String GREETINGS = "greetings";
	public final static String LOGIN = "login";
	protected final static String LOGOUT = "logout";
	public final static String REGISTRATION = "registrationForm";
	
	private String name, password, status, userId;
	private LoginManager loginManager = new LoginManager();
	
	Map<String, Object> session = ActionContext.getContext().getSession();
	
	@Action(value=GREETINGS)
	public String toGreetings(){
		log.info("MASUK LOGIN");
		log.info(name+" & "+password);
		String cek = loginManager.doConnect(name,password);
		log.info("cek = "+cek);
		log.info(url);
		if(null!=cek){
			session.put("logged_in", "true");
			session.put("screen_name", cek);
			setName(name);
			System.out.println("berhasil");
			return GREETINGS;
		}else{	
			if("true".equals(session.get("logged_in")))return GREETINGS;
			else{
				session.clear();
				//setStatus("login failed");
				log.info("GAGAL LOGIN");
				return LOGIN;
			}
		}
	}
	
	@Action(value=LOGOUT)
	public String logout() throws Exception {
	    session.clear();
	    session.put("logged_in", "false");
	    setStatus("logout");
	    return LOGIN;
	}
	
	@Action(value=LOGIN,interceptorRefs={@InterceptorRef("myInterceptStack")})
	public String execute(){
		log.info("login di controller");
		return LOGIN;
	}
	
	@Action(value=REGISTRATION)
	public String toRegistration(){
		log.info("masuk registrasi");
		return REGISTRATION;
	}

	public String submitRegistration(){
		boolean cek = loginManager.doRegister(name,password,userId);
		if(cek==true)
			return LOGIN;
		else
			return REGISTRATION;
	}
	
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}

	public String getUserId() {
		return userId;
	}

	public void setUserId(String userId) {
		this.userId = userId;
	}
	
}
